package com.kaiwukj.android.ufamily.mvp.ui.page.service.detail;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ServiceResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.service.ServiceContract;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;

public class ServiceDetailPresenter extends BasePresenter<ServiceContract.Model, ServiceContract.DetailView> {


    @Inject
    public ServiceDetailPresenter(ServiceContract.Model model, ServiceContract.DetailView view) {
        super(model, view);
    }

    public void fetchParams(int serviceId) {
        mModel.fetchServiceParams(serviceId)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<List<ServiceResult.ServiceInfo>>(mRootView) {
                    @Override
                    public void onSuccess(@NonNull List<ServiceResult.ServiceInfo> result) {
                        mRootView.onFetchServiceParams(result);
                    }
                });
    }

    public void getService(int serviceId) {
        mModel.getServiceResult(serviceId)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ServiceResult>(mRootView) {
                    @Override
                    public void onSuccess(@NonNull ServiceResult result) {
                        mRootView.onGetService(result);
                    }
                });

    }

    public void fetchData(int serviceId) {
        Observable.zip(mModel.getServiceResult(serviceId), mModel.fetchServiceParams(serviceId), (result, serviceInfos) -> {
            result.setServiceParamList(serviceInfos);
            return result;
        })
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ServiceResult>() {
                    @Override
                    public void onSuccess(@NonNull ServiceResult result) {
                        mRootView.onGetService(result);
                    }
                });
    }

}
